﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyAboutDataPump : ICompanyAboutAccess
    {
        public List<CRL.DataContract.DefaultDataPack> GetAubotInfo(string copName)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_about
                              where c.c_vc_cpname == copName
                              select new
                              {
                                  c.c_nvc_About,
                                  c.c_nvc_aboutImg,
                                  c.c_nvc_Address,
                                  c.c_nvc_Faith,
                                  c.c_nvc_LongName,
                                  c.c_nvc_Professional,
                                  c.c_nvc_Qualification,
                                  c.c_nvc_TitleContent,
                                  c.c_vc_email,
                                  c.c_vc_serviceline,
                                  c.c_vc_ZipCode
                              }).FirstOrDefault();
                if (result != null)
                {
                    return new List<DefaultDataPack>() { new DefaultDataPack() {
                        URL = result.c_nvc_aboutImg,
                        TITLE = result.c_nvc_LongName,
                        ABSTRACT = result.c_nvc_Faith,
                        EXTFIELD1 = result.c_nvc_TitleContent,
                        EXTFIELD2 = result.c_nvc_Professional,
                        EXTFIELD3 = result.c_nvc_Qualification,
                        EXTFIELD4 = result.c_nvc_About,
                        AddRESS = result.c_nvc_Address,
                        ZIPCODE = result.c_vc_ZipCode,
                        PHONE =result.c_vc_serviceline,
                        EXTFIELD5 = result.c_vc_email
                    } };
                }
                return new List<DefaultDataPack>() { new DefaultDataPack() { } };
            }
        }


        public bool UpdateAboutInfo(string copName, AboutDataPack pack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = (from c in ctx.tb_about
                            where c.c_vc_cpname == copName
                            select c).FirstOrDefault();
                if (item != null)
                {
                    if (!String.IsNullOrEmpty(pack.AboutContent))
                    {
                        item.c_nvc_About = pack.AboutContent;
                    }
                    if (!String.IsNullOrEmpty(pack.AboutUsImg))
                    {
                        item.c_nvc_aboutImg = pack.AboutUsImg;
                    }
                    if (!String.IsNullOrEmpty(pack.Address))
                    {
                        item.c_nvc_Address = pack.Address;
                    }
                    if (!String.IsNullOrEmpty(pack.Faith))
                    {
                        item.c_nvc_Faith = pack.Faith;
                    }
                    if (!String.IsNullOrEmpty(pack.Professional))
                    {
                        item.c_nvc_Professional = pack.Professional;
                    }
                     if (!String.IsNullOrEmpty(pack.Qualification))
                    {
                        item.c_nvc_Qualification = pack.Qualification;
                    }
                    if (!String.IsNullOrEmpty(pack.ServiceLine))
                    {
                        item.c_vc_serviceline = pack.ServiceLine;
                    }
                    if (!String.IsNullOrEmpty(pack.ZipCode))
                    {
                        item.c_vc_ZipCode = pack.ZipCode;
                    }
                    int affect = ctx.SaveChanges();
                    return affect == 1;
                }
            }
            return false;
        }
    }
}
